package org.gurms.dao.po.system;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "sys_menu")
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SysMenu implements Serializable {
	private String menuid;
	private String menuname;
	private String menuurl;
	private String menustatus;
	private String leafflag;
	private String remark;
	private int    menuorder;
	private int    privilegelist;

	private SysMenu parentmenu;

	private List<SysMenu> submenus = new ArrayList<SysMenu>();

	@Id
	@GeneratedValue(generator = "uuid")    
	@GenericGenerator(name = "uuid", strategy = "uuid") 
	public String getMenuid() {
		return menuid;
	}

	public void setMenuid(String menuid) {
		this.menuid = menuid;
	}

	@ManyToOne(
			cascade={CascadeType.PERSIST, CascadeType.MERGE},
			fetch=FetchType.LAZY
	)
	@JoinColumn(name="parentmenuid")
	public SysMenu getParentmenu() {
		return parentmenu;
	}

	public void setParentmenu(SysMenu parentmenu) {
		this.parentmenu = parentmenu;
	}

	@OneToMany(
			cascade={CascadeType.ALL},
			fetch=FetchType.LAZY,
			mappedBy="parentmenu"				
	)
	@OrderBy(value="menuorder desc")
	public List<SysMenu> getSubmenus() {
		return submenus;
	}

	public void setSubmenus(List<SysMenu> submenus) {
		this.submenus = submenus;
	}

	public String getMenuname() {
		return menuname;
	}

	public void setMenuname(String menuname) {
		this.menuname = menuname;
	}

	public String getMenuurl() {
		return menuurl;
	}

	public void setMenuurl(String menuurl) {
		this.menuurl = menuurl;
	}

	public int getPrivilegelist() {
		return privilegelist;
	}

	public void setPrivilegelist(int privilegelist) {
		this.privilegelist = privilegelist;
	}

	public String getMenustatus() {
		return menustatus;
	}

	public void setMenustatus(String menustatus) {
		this.menustatus = menustatus;
	}

	@Column(columnDefinition = "default '1'")
	public String getLeafflag() {
		return leafflag;
	}

	public void setLeafflag(String leafflag) {
		this.leafflag = leafflag;
	}

	@Column(columnDefinition = "default 0")
	public int getMenuorder() {
		return menuorder;
	}

	public void setMenuorder(int menuorder) {
		this.menuorder = menuorder;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public void addMenu(SysMenu menu){
		this.submenus.add(menu);
	}
	
	public boolean equals(Object o) {
		if (o == null || !(o instanceof SysMenu)) {
			return false;
		} else {
			SysMenu menu = (SysMenu) o;
			if (menu.getMenuid() == null) {
				return false;
			} else {
				return menu.getMenuid().equals(menuid);
			}
		}
	}

	public int hashCode() {
		if (menuid == null)
			return super.hashCode();
		return menuid.hashCode();
	}
}
